/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelCrearCargo.java
 *
 * Created on 9/04/2011, 04:24:02 PM
 */

package frontera.interfaz.panel;

import control.SIRHApp;
import entidad.Cargo;
import entidad.Departamento;
import entidad.Horario;
import frontera.interfaz.panel.herramientas.PanelHerramientasBuscar;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JComboBox;

/**
 *
 * @author Sebastian
 */
public class PanelCrearCargo extends javax.swing.JPanel {

    /** Creates new form PanelCrearCargo */
    public PanelCrearCargo() {
        departamentos = SIRHApp.getInstance().getSistema().getDepartamentos();
        iniciarArregloBox();
        initComponents();
    }

    private void iniciarArregloBox(){
        permisoActivo = "Gerente";
        arregloBox=new String[departamentos.size()];
        if(!departamentos.isEmpty()){
            int i=0;
            for(Departamento dep: departamentos){
                arregloBox[i]=dep.getNombre();                
                i++;
            }
            depActivo=departamentos.get(0);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        iconoCrear = new javax.swing.JLabel();
        titulo = new javax.swing.JLabel();
        botonGuardar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        labelResultado = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        tiposPermisos = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        panelFallas = new javax.swing.JPanel();
        modificarHorarioB = new javax.swing.JButton();
        panelDatosBasicos = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        labelNombre = new javax.swing.JLabel();
        fieldNombre = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        labelDepartamento = new javax.swing.JLabel();
        boxDepartamento = new JComboBox(arregloBox);
        jPanel5 = new javax.swing.JPanel();
        labelSalarioBasico = new javax.swing.JLabel();
        fieldSalarioBasico = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        boxEstado = new javax.swing.JComboBox();

        iconoCrear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/AddCargo128x128.png"))); // NOI18N

        titulo.setFont(new java.awt.Font("Dialog", 1, 32));
        titulo.setText("Creando Cargo");

        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Save.png"))); // NOI18N
        botonGuardar.setText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        botonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Cancelar.png"))); // NOI18N
        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        labelResultado.setText(" ");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nivel De Permisos"));
        jPanel1.setLayout(new java.awt.GridLayout(1, 2));

        jLabel3.setText("Tipo De Nivel De Permisos");
        jPanel1.add(jLabel3);

        tiposPermisos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ninguno", "Gerente", "Reclutamiento", "Auxiliar", "Coordinador" }));
        tiposPermisos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tiposPermisosActionPerformed(evt);
            }
        });
        jPanel1.add(tiposPermisos);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 223, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 25, Short.MAX_VALUE)
        );

        jPanel1.add(jPanel2);

        panelFallas.setBorder(javax.swing.BorderFactory.createTitledBorder("Horario"));

        modificarHorarioB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/scheduleSmall.png"))); // NOI18N
        modificarHorarioB.setText("Asignar");
        modificarHorarioB.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        modificarHorarioB.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        modificarHorarioB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarHorarioBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelFallasLayout = new javax.swing.GroupLayout(panelFallas);
        panelFallas.setLayout(panelFallasLayout);
        panelFallasLayout.setHorizontalGroup(
            panelFallasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFallasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(modificarHorarioB)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelFallasLayout.setVerticalGroup(
            panelFallasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(modificarHorarioB)
        );

        panelDatosBasicos.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Básicos"));
        panelDatosBasicos.setLayout(new java.awt.GridLayout(2, 2));

        labelNombre.setText("Nombre");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelNombre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(fieldNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNombre)
                    .addComponent(fieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        panelDatosBasicos.add(jPanel3);

        labelDepartamento.setText("Departamento");

        boxDepartamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxDepartamentoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelDepartamento)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boxDepartamento, 0, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDepartamento)
                    .addComponent(boxDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        panelDatosBasicos.add(jPanel4);

        labelSalarioBasico.setText("Salario Básico");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelSalarioBasico)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fieldSalarioBasico, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSalarioBasico)
                    .addComponent(fieldSalarioBasico, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        panelDatosBasicos.add(jPanel5);

        jLabel1.setText("Estado");

        boxEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vacante" }));
        boxEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxEstadoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(boxEstado, 0, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(boxEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        panelDatosBasicos.add(jPanel6);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(iconoCrear)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(titulo))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(panelFallas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(52, 52, 52))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(labelResultado, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(452, Short.MAX_VALUE)
                        .addComponent(botonGuardar)
                        .addGap(18, 18, 18)
                        .addComponent(botonCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panelDatosBasicos, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(iconoCrear))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(titulo)
                        .addGap(22, 22, 22)
                        .addComponent(panelFallas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelDatosBasicos, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(labelResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCancelar)
                    .addComponent(botonGuardar))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalCargo(SIRHApp.getInstance().getVentanaPrincipal().getPanelCargos());
        ((PanelHerramientasBuscar<Cargo>) SIRHApp.getInstance().getFactoryInterfaz().getPanelCargos().getPanelHerramientas()).inicializarBusqueda();
        SIRHApp.getInstance().getVentanaPrincipal().getPanelCargos().actualizarLista(SIRHApp.getInstance().getSistema().getCargos());
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        
        String resultadoOperacion = SIRHApp.getInstance().getAdmCargos().crearCargo(fieldNombre.getText(),horarioTemporal, fieldSalarioBasico.getText(), depActivo, permisoActivo, (String) boxEstado.getSelectedItem(),Calendar.getInstance());
        
        if(resultadoOperacion.equals("")){

            SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalCargo(SIRHApp.getInstance().getVentanaPrincipal().getPanelCargos());
            //Dejar la búsqueda del panel anterior en blanco
            ((PanelHerramientasBuscar<Cargo>) SIRHApp.getInstance().getFactoryInterfaz().getPanelCargos().getPanelHerramientas()).inicializarBusqueda();
            SIRHApp.getInstance().getVentanaPrincipal().getPanelCargos().actualizarLista(SIRHApp.getInstance().getSistema().getCargos());
        }
        else{
            labelResultado.setText(resultadoOperacion);
        }
    }//GEN-LAST:event_botonGuardarActionPerformed

    private void tiposPermisosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tiposPermisosActionPerformed
        JComboBox cb = (JComboBox)evt.getSource();
        permisoActivo = (String)cb.getSelectedItem();
    }//GEN-LAST:event_tiposPermisosActionPerformed

    private void modificarHorarioBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarHorarioBActionPerformed
 
        String nombre = fieldNombre.getText();
        PanelHorarioCargo panelHorario = new PanelHorarioCargo(nombre, horarioTemporal, this);

        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalCargo(panelHorario);

}//GEN-LAST:event_modificarHorarioBActionPerformed

    private void boxDepartamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxDepartamentoActionPerformed
        if (!departamentos.isEmpty()) {
            JComboBox cb = (JComboBox) evt.getSource();
            int indice = cb.getSelectedIndex();
            depActivo = departamentos.get(indice);
        }
}//GEN-LAST:event_boxDepartamentoActionPerformed

    private void boxEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxEstadoActionPerformed
        
}//GEN-LAST:event_boxEstadoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCancelar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JComboBox boxDepartamento;
    private javax.swing.JComboBox boxEstado;
    private javax.swing.JTextField fieldNombre;
    private javax.swing.JTextField fieldSalarioBasico;
    private javax.swing.JLabel iconoCrear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JLabel labelDepartamento;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel labelResultado;
    private javax.swing.JLabel labelSalarioBasico;
    private javax.swing.JButton modificarHorarioB;
    private javax.swing.JPanel panelDatosBasicos;
    private javax.swing.JPanel panelFallas;
    private javax.swing.JComboBox tiposPermisos;
    private javax.swing.JLabel titulo;
    // End of variables declaration//GEN-END:variables
    private Departamento depActivo;
    private ArrayList<Departamento> departamentos;
    private Cargo cargoTemporal;
    private Horario horarioTemporal = new Horario();
    private String[] arregloBox;
    private String permisoActivo;
}
